package com.kinglone.back.user.realm;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import com.kinglone.back.module.service.ModuleService;
import com.kinglone.back.permission.service.PermissionService;
import com.kinglone.back.user.model.User;
import com.kinglone.back.user.model.UserPopedom;
import com.kinglone.back.user.service.UserService;

/**
 * 自定义realm
 * @author Administrator
 *
 */
public class MyRealm extends AuthorizingRealm{

	@Resource
	private UserService userService;
	@Autowired
	private PermissionService permissionService;
	@Autowired
	private ModuleService moduleService;
	
	/**
	 * 为当限前登录的用户授予角色和权
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		String username = (String) principals.getPrimaryPrincipal();
		System.out.println("username:"+username);
		return null;
	}

	/**
	 * 验证当前登录的用户
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
								
		 UsernamePasswordToken token = (UsernamePasswordToken) authcToken;  
		 String accountName = token.getUsername();  
		 System.out.println("------accountName:"+accountName);
		
		if(!StringUtils.isEmpty(accountName)){  
            User user = userService.getByUserName(accountName);  
            if(user != null){           
          //根据用户id关联角色取出菜单
            List<UserPopedom> userPopedoms  = moduleService.findOperasModules(user);
       		//model.addAttribute("userPopedoms", userPopedoms);
    		//List<Permission> menus  = userService.findMenuListByUserId(user.getId());
    		System.out.println("----menus:"+userPopedoms);
    		//user.setMenus(menus);
    		setSession("user", user);
    		setSession("userPopedoms",userPopedoms);
            AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user, user.getPassword(), "xx");           
            return authenticationInfo;
            }  
        }  	
			return null;	
	}
	/**
	 * 将一些数据放到ShiroSession中,以便于其它地方使用
	 * 
	 * @see 比如Controller,使用时直接用HttpSession.getAttribute(key)就可以取到
	 */
	private void setSession(Object key, Object value) {
		Subject currSubject = SecurityUtils.getSubject();
		if (currSubject != null) {
			Session session = currSubject.getSession();
			if (session != null) {
				session.setAttribute(key, value);
			}
		}

	}

}
